package com.AAA.service.Impl.sds;


import com.AAA.dao.sds.ITSdsMapper;
import com.AAA.entity.*;
import com.AAA.entity.mht.TMht;
import com.AAA.entity.sds.TSds;
import com.AAA.service.sds.ITsdsService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class TsdsServiceimpl implements ITsdsService {

    @Autowired
    private ITSdsMapper mapper;
    //分页查询
    @Override
    public PageInfo query(MyPage myPage, TSds tSds) {
        PageHelper.startPage(myPage.getPage(),myPage.getLimit());
        List<TSds> tSds1 = mapper.queryall(tSds);
        PageInfo pageInfo = new PageInfo(tSds1);
        return pageInfo;
    }
    //批量添加
    @Override
    public Integer addall(TSds tSds) {
        int insert = mapper.insert(tSds);
        if(insert >0){
            return 1;
        }
        return 0;
    }
    //添加
    @Override
    public boolean add(TSds tSds) {
        int insert = mapper.insert(tSds);
        if (insert>0){
            return true;
        }
        return false;
    }
    //删除
    @Override
    public boolean delete(Integer qid) {
        //deleteByPrimaryKey 获取实体类定义的id值  进行删除
        int insert = mapper.deleteByPrimaryKey(qid);
        if (insert>0){
            return true;
        }
        return false;
    }
    @Override
    public TSds selectById(Integer qid) {

        return mapper.selectByPrimaryKey(qid);
    }
    //修改
    @Override
    public boolean edit(TSds tSds) {
        int insert=mapper.updateByPrimaryKeySelective(tSds);
        if(insert>0){
            return true;
        }
        return false;
    }



    @Override
    public void save(List<TSds> list) {
        mapper.insertfsdsjects(list);
    }

    @Override
    public void dealFile(MultipartFile file) {

        // 如果试题已经存在就不让再导入了
        int i = mapper.selectCount(null);
        if(i>0){
            return;
        }else {
            // 将excel中的数据导入到数据库中
//      InputStream is = null;
//         String fileUrl="https://yuyonglitest.oss-cn-beijing.aliyuncs.com/%E9%A2%98%E7%9B%AE%E6%B8%85%E5%8D%952.xlsx";
//      try {
//         is = new URL(fileUrl).openStream();
//      } catch (IOException e) {
//         e.printStackTrace();
//      }
            InputStream is = null;
            try {
                is = file.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //1.进行读取数据，TFpaSub是我的pojo类，
            //2.new FileListener(fileService)这个是监听器，主要用来读取数据的
            //3.特别注意的是fileService这个service，我上面有注入进去 @Autowired/@Resource，切记不要new会报错
            //EasyExcel.read(is, TFpaSub.class, new FileListener(fileService)).sheet("FPA性格测试").doRead();
            EasyExcel.read(is, TSds.class, new AnalysisEventListener<TSds>() {
                /**
                 * 单次缓存的数据量
                 */
                public static final int BATCH_COUNT = 100;
                /**
                 * 临时存储
                 */
                private List<TSds> cachedDataList = new ArrayList<>();

                @Override
                public void invoke(TSds data, AnalysisContext context) {
                    cachedDataList.add(data);
                    if (cachedDataList.size() >= BATCH_COUNT) {
                        saveData();
                        // 存储完成清理 list
                        cachedDataList = new ArrayList<>();
                    }
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext context) {
                    saveData();
                }

                /**
                 * 加上存储数据库
                 */
                private void saveData() {
//            log.info("{}条数据，开始存储数据库！", cachedDataList.size());
//            log.info("存储数据库成功！");
                    save(cachedDataList);
                }
            }).sheet("FPA性格测试").doRead();
        }
    }
    @Override
    public PageInfo getAll(MyPage myPage) {

        PageHelper.startPage(myPage.getPage(),myPage.getLimit());

        return new PageInfo(mapper.selectAll());
    }
}